package com.ocean.array;

import java.util.List;

public class WaterContainer {

    public static void main(String[] args) {
        int[] height = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7};
//        int[] height = new int[]{1, 1};
        System.out.println(new WaterContainer().maxArea(height));
    }


    public int maxArea(int[] height) {
        int i = 0;
        int j = height.length - 1;
        int maxArea = 0;
        while (i < j) {
            int width = j - i;
            int h = Math.min(height[i], height[j]);
            // 算出当前的面积
            int current = Math.multiplyExact(width, h);
            maxArea = Math.max(current, maxArea);
            if (height[i] > height[j]) {
                j--;
            } else {
                i++;
            }
        }
        return maxArea;
    }

    public int maxArea2(int[] height) {
        int l = 0;
        int r = height.length - 1;
        int max = 0;
        while (l < r) {
            int min = Math.min(height[l], height[r]);
            int temp = min * (r - l);
            max = Math.max(temp, max);
            // 可以连续跳过
            while (height[l] <= min && l < r) {
                l++;
            }
            while (height[r] <= min && l < r) {
                r--;
            }

        }
        return max;
    }
}
